package com.ihr360.oneclick.dto.spreadsheet;


import com.ihr360.commons.model.entity.StringIdWithTime;
import com.ihr360.oneclick.dto.OneClickTermDto;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 * 用户权限
 */
@Entity()
@Table(name = "tab_user_spreadsheet",
        indexes = {
                @Index(name = "tab_user_spreadsheet_idx", columnList = "term_id,userId,spreadsheet_id", unique = true)})
@DynamicUpdate
public class UserSpreadsheetDto extends StringIdWithTime {
    public static class SPREAD_SHEET_SCOPE {
        public final static int READ_ONLY_SCOPE_MASK = 0x001;
        public final static int WRITE_ONLY_SCOPE_MASK = 0x010;
        public final static int EXECUTE_ONLY_SCOPE_MASK = 0x100;
    }

    /**
     * i人事里的用户id
     */
    @Column(length = 40, nullable = false)
    private String userId;
    /**
     *
     */
    @OneToOne
    @JoinColumn(name = "spreadsheet_id", nullable = false)
    private SpreadsheetDto spreadsheet;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "term_id")
    private OneClickTermDto term;

    /**
     * 用户对spreadsheet的操作权限表
     * scope
     */
    @Column(length = 8, nullable = false)
    private int scope;


    public UserSpreadsheetDto() {
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public SpreadsheetDto getSpreadsheet() {
        return spreadsheet;
    }

    public void setSpreadsheet(SpreadsheetDto spreadsheet) {
        this.spreadsheet = spreadsheet;
    }

    public OneClickTermDto getTerm() {
        return term;
    }

    public void setTerm(OneClickTermDto term) {
        this.term = term;
    }

    public int getScope() {
        return scope;
    }

    public void setScope(int scope) {
        this.scope = scope;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;

        UserSpreadsheetDto that = (UserSpreadsheetDto) o;

        if (scope != that.scope) return false;
        if (userId != null ? !userId.equals(that.userId) : that.userId != null) return false;
        return !(spreadsheet != null ? !spreadsheet.equals(that.spreadsheet) : that.spreadsheet != null);

    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (userId != null ? userId.hashCode() : 0);
        result = 31 * result + (spreadsheet != null ? spreadsheet.hashCode() : 0);
        result = 31 * result + scope;
        return result;
    }
}